package com.geek.framework.study.config.microprofile;

import com.geek.framework.study.config.microprofile.converter.StringConversionService;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;


public class DefaultConfigBuilder implements ConfigBuilder {


    private volatile boolean addDefaultConfigSource;

    private ConfigSources configSources;

    private StringConversionService conversionService;

    public DefaultConfigBuilder(ClassLoader classLoader) {
        this.configSources = new ConfigSources(classLoader);
        conversionService = StringConversionService.getSharedInstance();
        this.addDefaultSources()
                .addDiscoveredConverters();
    }

    @Override
    public ConfigBuilder addDefaultSources() {
        configSources.addDefaultSources();
        return this;
    }

    @Override
    public ConfigBuilder addDiscoveredSources() {
        configSources.addDiscoveredSources();
        return this;
    }

    @Override
    public ConfigBuilder addDiscoveredConverters() {
        //spi的已经加载过了 .. 如果有自定义的,应该这里进行添加
        return this;
    }

    @Override
    public ConfigBuilder forClassLoader(ClassLoader classLoader) {
        return this;
    }

    @Override
    public ConfigBuilder withSources(ConfigSource... configSources) {
        this.configSources.addConfiSource(configSources);
        return this;
    }

    @Override
    public ConfigBuilder withConverters(Converter<?>... converters) {
        this.conversionService.addConverter(converters);
        return this;
    }

    @Override
    public <T> ConfigBuilder withConverter(Class<T> aClass, int i, Converter<T> converter) {
        return this;
    }

    @Override
    public Config build() {
        return new DefaultConfig(configSources, conversionService);
    }




}
